require 'digest/md5'
class Comment < ActiveRecord::Base
  default_scope :order => 'created_at DESC'
  belongs_to :post, :counter_cache => true
  belongs_to :user
  named_scope :recent_comments, :limit => 10, :order => 'created_at DESC'
  validates_presence_of :username, :email, :content, :message => "不能为空!"
  
  # => 留言者的头像
  def avatar
    "http://www.gravatar.com/avatar/#{Comment.email_to_md5(self.email)}?s=40"
  end

  def self.email_to_md5(email)
    email.strip!
    Digest::MD5.hexdigest(email)
  end
  
end
